package userInterface;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Auction;
import model.Control;
import model.NPO;
import model.NPOStaff;

/**
 * This class take information of Auction.
 * 
 * @author fahad Muzaffar
 * @author Daniel Bartholomew
 * 
 */
@SuppressWarnings("serial")
public class AuctionForm extends JFrame {
	
	private class cancelbuttons implements ActionListener {
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			dispose();
		}
	}

	private class submitbuttons implements ActionListener {
		
		/**
		 * @author Daniel Bartholomew
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Calendar date = new GregorianCalendar();
			try {
				date.setTime(date_format.parse(date_textfield.getText()));
				
			} catch (ParseException e) {
				JOptionPane.showMessageDialog(null,	"Bad Date");
				return;
			}
			Calendar time = new GregorianCalendar();
			try {
				time.setTime(time_format.parse(start_textfield.getText()));
				date.set(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY));
				date.set(Calendar.MINUTE, time.get(Calendar.MINUTE));
			} catch (ParseException e) {
				JOptionPane.showMessageDialog(null,	"Bad Time");
				return;
			}
			double duration;
			try {
				duration = Double.parseDouble(duration_textfield.getText());
				if (duration < .1) {
					JOptionPane.showMessageDialog(null,	"Duration too short");
					return;
				} else if(duration > 10) {
					JOptionPane.showMessageDialog(null,	"Duration too long");
					return;
				}
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(null,	"Bad Duration");
				return;
			}
			String response;
			response = my_control.addAuction(date, duration);
			JOptionPane.showMessageDialog(null,	response);
			Collection<Auction> auctions = my_control.getAuctions();
			NPO npo = ((NPOStaff) my_control.getActiveUser()).getNPO();
			for (Auction a : auctions) {
				if (a.getNPO().equals(npo)) {
					new AuctionView(a, my_control);
					dispose();
					break;
				}
			}
		}
	}

	public static void main(String... args) {
		new AuctionForm(new Control());
	}

	private static DateFormat date_format = DateFormat.getDateInstance(DateFormat.MEDIUM);
	
    private static DateFormat time_format = new SimpleDateFormat("hh:mm");
    
    private Control my_control;
	
	private JTextField date_textfield;
	
	private JTextField start_textfield;
	
	private JTextField duration_textfield;
		
	/**
	 * Constructor to display a GUI for auction forms. Constructor
	 */
	public AuctionForm(Control control) {
		super("Auction Form");
		my_control = control;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		pack();
		setSize(600, 580);
		JPanel top = new JPanel();
		top.setLayout(new GridLayout(8, 1));

		JPanel display_auction_date = new JPanel();
		JLabel auction_date = new JLabel("Auction Date (Month Day, Year):");
		display_auction_date.add(auction_date);
		date_textfield = new JTextField(30);
		display_auction_date.add(date_textfield);

		JPanel display_auction_start_time = new JPanel();
		JLabel start_auction = new JLabel("Auction Start Time (24:60):");
		display_auction_start_time.add(start_auction);
		start_textfield = new JTextField(30);
		display_auction_start_time.add(start_textfield);

		JPanel display_auction_duration_time = new JPanel();
		JLabel auction_duration = new JLabel("Auction Duration(# of hours):");
		display_auction_duration_time.add(auction_duration);
		duration_textfield = new JTextField(30);
		display_auction_duration_time.add(duration_textfield);

		top.add(display_auction_date);
		top.add(display_auction_start_time);
		top.add(display_auction_duration_time);

		JPanel bottom = new JPanel();
		bottom.setLayout(new BorderLayout());

		JButton submit = new JButton("Submit");
		JPanel display_buttons = new JPanel();
		display_buttons.add(submit);
		JButton cancel = new JButton("Cancel");
		display_buttons.add(cancel);
		display_buttons.setLayout(new GridLayout(1, 2));
		bottom.add(display_buttons, BorderLayout.SOUTH);

		cancel.addActionListener(new cancelbuttons());
		submit.addActionListener(new submitbuttons());
		setLayout(new GridLayout(2, 1));
		add(top);
		add(bottom);
		pack();
		setLocationRelativeTo(null);
	}
}